﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NLog;
using System.Data.SqlClient;

namespace CodeFarmer.Core.Utility
{
    /// <summary>
    /// 日志记录类
    /// </summary>
    public class LogHelper
    {
        private static bool isinit;
        private static bool LogInfoEnable;
        private static bool LogErrorEnable;
        private static bool LogExceptionEnable;
        private static bool LogComplementEnable;
        private static bool LogDubugEnable;
        private static Logger logger;
        static LogHelper()
        {
            LogHelper.isinit = false;
            LogHelper.LogInfoEnable = false;
            LogHelper.LogErrorEnable = false;
            LogHelper.LogExceptionEnable = false;
            LogHelper.LogComplementEnable = false;
            LogHelper.LogDubugEnable = false;
            LogHelper.logger = LogManager.GetCurrentClassLogger();
            if (!LogHelper.isinit)
            {
                LogHelper.isinit = true;
                LogHelper.SetConfig();
            }
        }
        /// <summary>
        /// 设置初始值。
        /// </summary>
        public static void SetConfig()
        {
            LogHelper.LogInfoEnable = LogHelper.logger.IsInfoEnabled;
            LogHelper.LogErrorEnable = LogHelper.logger.IsErrorEnabled;
            LogHelper.LogExceptionEnable = LogHelper.logger.IsErrorEnabled;
            LogHelper.LogComplementEnable = LogHelper.logger.IsTraceEnabled;
            LogHelper.LogDubugEnable = LogHelper.logger.IsDebugEnabled;
        }
        /// <summary>
        /// 写入普通日志消息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteInfo(string info)
        {
            if (LogHelper.LogInfoEnable)
            {
                LogHelper.logger.Info(LogHelper.BuildMessage(info));
            }
        }
        /// <summary>
        /// 写入Debug日志消息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteDebug(string info)
        {
            if (LogHelper.LogDubugEnable)
            {
                LogHelper.logger.Debug(LogHelper.BuildMessage(info));
            }
        }
        /// <summary>
        /// 写入错误日志消息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteError(string info)
        {
            if (LogHelper.LogErrorEnable)
            {
                LogHelper.logger.Error(LogHelper.BuildMessage(info));
            }
        }
        /// <summary>
        /// 写入异常日志信息
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ex"></param>
        public static void WriteException(string info, Exception ex)
        {
            if (LogHelper.LogExceptionEnable)
            {
                LogHelper.logger.Error(LogHelper.BuildMessage(info, ex));
            }
        }
        /// <summary>
        /// 写入严重错误日志消息
        /// </summary>
        /// <param name="info"></param>
        public static void WriteFatal(string info)
        {
            if (LogHelper.LogErrorEnable)
            {
                LogHelper.logger.Fatal(LogHelper.BuildMessage(info));
            }
        }
        /// <summary>
        /// 写入补充日志
        /// </summary>
        /// <param name="info"></param>
        public static void WriteComplement(string info)
        {
            if (LogHelper.LogComplementEnable)
            {
                LogHelper.logger.Trace(LogHelper.BuildMessage(info));
            }
        }
        /// <summary>
        /// 写入补充日志
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ex"></param>
        public static void WriteComplement(string info, Exception ex)
        {
            if (LogHelper.LogComplementEnable)
            {
                LogHelper.logger.Trace(LogHelper.BuildMessage(info, ex));
            }
        }
        private static string BuildMessage(string info)
        {
            return LogHelper.BuildMessage(info, null);
        }
        private static string BuildMessage(string info, Exception ex)
        {
            StringBuilder sb = new StringBuilder();
            HttpContext ctx = HttpContext.Current;
            sb.AppendFormat("Time: {0}{1}\r\n", DateTime.Now, info);
            if (ctx != null)
            {
                sb.AppendFormat("UserHostAddress: {0}\r\n", ctx.Request.UserHostAddress);
                sb.AppendFormat("WebServer: {0}\r\n", ctx.Request.ServerVariables["LOCAL_ADDR"]);
                sb.AppendFormat("Url: {0}\r\n", ctx.Request.Url);
                if (null != ctx.Request.UrlReferrer)
                {
                    sb.AppendFormat("UrlReferrer: {0}\r\n", ctx.Request.UrlReferrer);
                }
            }
            if (ex != null)
            {
                if (ex is SqlException)
                {
                    sb.AppendFormat("Database: {0}\r\n", ((SqlException)ex).Server);
                }
                sb.AppendFormat("Exception: {0}\r\n", ex);
            }
            sb.AppendLine();
            return sb.ToString();
        }

    }
}
